package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.query.MPJQueryWrapper;
import com.mdd.admin.service.ILegalUserWithdrawService;
import com.mdd.admin.validate.LegalUserWithdrawSearchValidate;
import com.mdd.admin.validate.LegalUserWithdrawStatusUpdateValidate;
import com.mdd.admin.validate.LegalUserWithdrawUpdateValidate;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.vo.LegalUserWithdrawDetailVo;
import com.mdd.admin.vo.LegalUserWithdrawListedVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.LegalUserWithdraw;
import com.mdd.common.mapper.LegalUserWithdrawMapper;
import com.mdd.common.util.StringUtils;
import com.mdd.common.util.TimeUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;

/**
 * 提现表实现类
 * @author LikeAdmin
 */
@Service
public class LegalUserWithdrawServiceImpl implements ILegalUserWithdrawService {
        
    @Resource
    LegalUserWithdrawMapper legalUserWithdrawMapper;

    /**
     * 提现表列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<LegalUserWithdrawListedVo>
     */
    @Override
    public PageResult<LegalUserWithdrawListedVo> list(PageValidate pageValidate, LegalUserWithdrawSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        MPJQueryWrapper<LegalUserWithdraw> mpjQueryWrapper = new MPJQueryWrapper<>();
        mpjQueryWrapper.selectAll(LegalUserWithdraw.class).select("t2.real_name as realName, t2.mobile")
                .leftJoin("la_user t2 on t.user_id = t2.id")
                .orderByDesc("t.id");
        if (StringUtils.isNotEmpty(searchValidate.getKeyword())) {
            String keyword = searchValidate.getKeyword();
            mpjQueryWrapper.nested(wq->wq
                    .like("t2.real_name", keyword).or()
                    .like("t2.mobile", keyword));
        }
        legalUserWithdrawMapper.setSearch(mpjQueryWrapper, searchValidate, new String[]{
                "=:status:int",
        });

        IPage<LegalUserWithdrawListedVo> iPage = legalUserWithdrawMapper.selectJoinPage(
                new Page<>(page, limit), LegalUserWithdrawListedVo.class, mpjQueryWrapper);
        return PageResult.iPageHandle(iPage);
    }

    /**
     * 提现表详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return LegalUserWithdraw
     */
    @Override
    public LegalUserWithdrawDetailVo detail(Integer id) {

        MPJQueryWrapper<LegalUserWithdraw> mpjQueryWrapper = new MPJQueryWrapper<>();
        mpjQueryWrapper.selectAll(LegalUserWithdraw.class).select(
                "t2.real_name as realName, t2.mobile, t2.username, " +
                        "t3.username as bankUserName, t3.card_number as cardNumber, t3.bank_name as bankName, t3.mobile as bankMobile, " +
                        "t4.nickname as checkUserName")
                .leftJoin("la_user t2 on t.user_id = t2.id")
                .leftJoin("legal_user_account_bank t3 on t.card_id = t3.id")
                .leftJoin("business_user t4 on t.check_user = t4.id")
                .eq("t.id", id)
                .orderByDesc("t.id");

        LegalUserWithdrawDetailVo vo = legalUserWithdrawMapper.selectJoinOne(
                LegalUserWithdrawDetailVo.class, mpjQueryWrapper);
        Assert.notNull(vo, "数据不存在");
        if (vo != null) {
            if (vo.getCreateTime() != null) {
                vo.setCreateTimeStr(TimeUtils.timestampToDate(vo.getCreateTime()));
            }
            if (vo.getCheckTime() != null) {
                vo.setCheckTimeStr(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(vo.getCheckTime()));
            }
        }
        return vo;
    }

    /**
     * 提现表编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(LegalUserWithdrawUpdateValidate updateValidate) {
        LegalUserWithdraw model = legalUserWithdrawMapper.selectOne(
                new QueryWrapper<LegalUserWithdraw>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setUserId(updateValidate.getUserId());
        model.setAmount(updateValidate.getAmount());
        model.setCardId(updateValidate.getCardId());
        model.setCheckTime(updateValidate.getCheckTime());
        model.setCheckUser(updateValidate.getCheckUser());
        model.setStatus(updateValidate.getStatus());
        model.setTradeNo(updateValidate.getTradeNo());
        model.setRemark(updateValidate.getRemark());
        legalUserWithdrawMapper.updateById(model);
    }

    @Override
    public void editStatus(LegalUserWithdrawStatusUpdateValidate updateValidate) {
        LegalUserWithdraw model = legalUserWithdrawMapper.selectOne(
                new QueryWrapper<LegalUserWithdraw>()
                        .eq("id",  updateValidate.getId())
                        .last("limit 1"));

        Assert.notNull(model, "数据不存在!");
        model.setStatus(updateValidate.getStatus());
        legalUserWithdrawMapper.updateById(model);
    }

}
